<?php
/**
 * 遮罩后台管理
 *
 * 遮罩后台管理service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\MasksService;
use Illuminate\Support\Facades\DB;
use App\Services\BaseService;
use App\Services\ImgServices\ImgTool;
class MasksBackService extends BaseService
{
    public function __construct(ImgTool $img_tool)
    {
        $this->img_tool = $img_tool;
    }

    /**
     * 获取遮罩列表
     * @param int $per_page
     * @return mixed
     */
    public function getList($per_page = 10){
        $this->setParameter([
            'table' => 'masks',
            'where' => [['state','=',1]],
            'select'=> ['mask_id','original_image','thumbnail','mask_preview_image','created_at'],
            'order' => 'created_at',
            'sort'  => 'desc'
        ]);
        $masks = $this->getAllPaginated($per_page);
        foreach($masks as $mask){
            $mask->original_image = env('IMG_URL_STD_PREFIX').$mask->original_image;
            $mask->thumbnail = env('IMG_URL_STD_PREFIX').$mask->thumbnail;
            $mask->mask_preview_image = env('IMG_URL_STD_PREFIX').$mask->mask_preview_image;
        }
        return $masks;
    }

    /**
     * 保存遮罩文件
     * @param $data
     * @return mixed
     */
    public function add_mask($data){
        $data['original_image'] = $this->img_tool->imgSave($data['original_image'],2);//原图
        if(empty($data['original_image'])){
            return false;
        }
        //缩略图
        $save_path_thumbnail = $this->img_tool->getImgThumbByImgPath($data['original_image'],500, 500);
        if(empty($save_path_thumbnail)){
            return false;
        }
        $data['thumbnail'] = $save_path_thumbnail;
        $data['mask_preview_image'] = $this->img_tool->imgSave($data['mask_preview_image'],2);
        if(empty($data['mask_preview_image'])){
            return false;
        }
        $data['created_at'] = $data['updated_at'] = time();
        $flag = DB::table('masks')->insert($data);
        return $flag;
    }

    /**
     * 修改
     * @param $data
     * @return bool
     */
    public function update_mask($data){
        if(!empty($data['original_image'])){
            $data['original_image'] = $this->img_tool->imgSave($data['original_image'],2);//原图
            if(empty($data['original_image'])){
                return false;
            }
            //缩略图
            $save_path_thumbnail = $this->img_tool->getImgThumbByImgPath($data['original_image'],500, 500);
            if(empty($save_path_thumbnail)){
                return false;
            }
            $data['thumbnail'] = $save_path_thumbnail;
        }
        if(!empty($data['mask_preview_image'])){
            $data['mask_preview_image'] = $this->img_tool->imgSave($data['mask_preview_image'],2);
            if(empty($data['mask_preview_image'])){
                return false;
            }
        }
        $data['updated_at'] = time();
        $flag = DB::table('masks')->where('mask_id',$data['mask_id'])->update($data);
        return $flag;
    }

    /**
     * 删除
     * @param $mask_id
     * @return mixed
     */
    public function remove_mask($mask_id){
        $flag = DB::table('masks')->where('mask_id', $mask_id)->update(['state' => 2,'updated_at'=>time()]);
        return $flag;
    }

    /**
     * 获取
     * @param $mask_id
     * @return mixed
     */
    public function get_mask_by_id($mask_id){
        //获取类型详细信息
        $this->setParameter([
            'table' => 'masks',
            'where' => [['mask_id','=',$mask_id],['state','=',1]],
            'select'=> ['mask_id','original_image','thumbnail','mask_preview_image','created_at']
        ]);
        $mask = $this->getById();
        if(!empty($mask)){
            $mask->original_image = env('IMG_URL_STD_PREFIX').$mask->original_image;
            $mask->thumbnail = env('IMG_URL_STD_PREFIX').$mask->thumbnail;
            $mask->mask_preview_image = env('IMG_URL_STD_PREFIX').$mask->mask_preview_image;
        }
        return $mask;
    }
}